from PyQt5 import QtCore,QtWidgets,QtGui
from PyQt5.QtCore import QObject,QPropertyAnimation,QRect,QPoint,QVariant
from PyQt5.QtWidgets import QWidget,QPushButton
from PyQt5.QtGui import QBrush,QPixmap,QPainter,QFont,QFontMetrics,QColor
import lowmainres
import trayiconmenures
import upmainres
import centerwindres
class MainButton(QPushButton):
    def __init__(self,strpixnormal,strpixenter,strpixleave,parent=None):
        super(MainButton, self).__init__(parent)
        pixnormal = QPixmap(strpixnormal)
        pixenter = QPixmap(strpixenter)
        pixleave = QPixmap(strpixleave)

        self.setCursor(QtCore.Qt.PointingHandCursor)
        self.leave = False
        self.enter = True
        self.leaveindex = 0
        self.enterindex = 0
        self.pixnormal = pixnormal
        self.enterlist = []
        self.leavelist = []
        for i in range(10):
            self.enterlist.append(pixenter.copy(i*(pixenter.width()/10),0,pixenter.width()/10,pixenter.height()))
        for j in range(8):
            self.leavelist.append(pixleave.copy(j*(pixleave.width()/8),0,pixleave.width()/8,pixleave.height()))
        self.enteranimation = QPropertyAnimation(self,b"")
        self.enteranimation.setStartValue(0)
        self.enteranimation.setEndValue(9)
        self.enteranimation.setDuration(600)
        self.enteranimation.valueChanged.connect(self.entervaluechange)

        self.leaveanimation = QPropertyAnimation(self,b"")
        self.leaveanimation.setStartValue(0)
        self.leaveanimation.setEndValue(7)
        self.leaveanimation.setDuration(600)
        self.leaveanimation.valueChanged.connect(self.leavevaluechange)

    def enterEvent(self, a0: QtCore.QEvent):
        self.enter = True
        self.leave = False
        self.enteranimation.start()
    def leaveEvent(self, a0: QtCore.QEvent):
        self.enter = False
        self.leave = True
        self.leaveanimation.start()

    def paintEvent(self, a0: QtGui.QPaintEvent):
        painter = QPainter(self)
        if self.enter:
            painter.drawPixmap(self.rect(), self.enterlist[self.enterindex])
        else:
            painter.drawPixmap(self.rect(), self.leavelist[self.leaveindex])
    def entervaluechange(self,var):
        self.enterindex = var
        self.update()
    def leavevaluechange(self,var):
        self.leaveindex = var
        self.update()



class MainButton2(QPushButton):
    def __init__(self,pixnormal,text,parent=None):
        super(MainButton2, self).__init__(parent)
        self.setCursor(QtCore.Qt.PointingHandCursor)
        self.pix = QPixmap(pixnormal)
        self.text = text
        self.enter = False
        borpix = QPixmap(":/image/btnborder.png")
        self.pixborder = borpix.copy(2*(borpix.width()/3),0,borpix.width()/3,borpix.height())

    def paintEvent(self, a0: QtGui.QPaintEvent):
        painter = QPainter(self)
        if self.enter:
            painter.drawPixmap(self.rect(),self.pixborder)
        painter.drawPixmap(QRect((self.rect().width()-self.pix.width())/2,(self.rect().height()-self.pix.height())/3,self.pix.width(),self.pix.height()),self.pix)
        font = QFont()
        fontmetr = QFontMetrics(font)
        self.width = fontmetr.width(self.text)

        painter.setPen(QColor(124,124,124))
        painter.drawText(QPoint((self.rect().width()-self.width)/2,(self.rect().height() - self.pix.height())/3+self.pix.height() + 20),self.text)

    def enterEvent(self, a0: QtCore.QEvent):
        self.enter = True
        self.update()
    def leaveEvent(self, a0: QtCore.QEvent):
        self.leave = False
        self.update()

class MainButton3(QPushButton):
    def __init__(self,pix_listurl,parent=None):
        super(MainButton3, self).__init__(parent)
        self.index = 0
        self.enter = False
        pix = QPixmap(pix_listurl)
        self.pixlist = []
        for i in range(4):
            self.pixlist.append(pix.copy(i*(pix.width()/4),0,pix.width()/4,pix.height()))

    def paintEvent(self, a0: QtGui.QPaintEvent):
        painter = QPainter(self)
        painter.drawPixmap((self.width() - self.pixlist[self.index].width()) / 2,
                           (self.height() - self.pixlist[self.index].height()) / 2
                           , self.pixlist[self.index].width()
                           , self.pixlist[self.index].height(), self.pixlist[self.index])

            
    def enterEvent(self, a0: QtCore.QEvent):
        self.index = 1
        self.enter = True
        self.update()

    def leaveEvent(self, a0: QtCore.QEvent):
        self.index = 0
        self.enter = False
        self.update()

    def mousePressEvent(self, e: QtGui.QMouseEvent):
        if e.button() == QtCore.Qt.LeftButton:
            self.index = 2
            self.update()
            super(MainButton3, self).mousePressEvent(e)
    def mouseReleaseEvent(self, e: QtGui.QMouseEvent):
        if e.button() == QtCore.Qt.LeftButton:
            self.index = 1
            self.update()
            super(MainButton3, self).mouseReleaseEvent(e)

class HeadButton(QPushButton):
    def __init__(self,parent=None):
        super(HeadButton, self).__init__(parent)
        self.setCursor(QtCore.Qt.PointingHandCursor)
        self.index = 0
        pixheader = QPixmap(":/image/head_bkg.png")
        self.pixlist = []
        self.pixlist.append(pixheader.copy(0,0,pixheader.width()/2,pixheader.height()))
        self.pixlist.append(pixheader.copy(pixheader.width()/2,0,pixheader.width()/2,pixheader.height()))

    def paintEvent(self, a0: QtGui.QPaintEvent):
        painter = QPainter(self)
        painter.drawPixmap(self.rect(),self.pixlist[self.index])
        pixhead = QPixmap(":/image/head.png")
        painter.drawPixmap((self.width()-pixhead.width())/2,(self.height()-pixhead.height())/2,pixhead.width(),pixhead.height(),pixhead)


    def enterEvent(self, a0: QtCore.QEvent):
        self.index = 1
        self.update()

    def leaveEvent(self, a0: QtCore.QEvent):
        self.index = 0
        self.update()

class WordsLineButton(QPushButton):
    def __init__(self,text,parent=None):
        super(WordsLineButton, self).__init__(parent)
        self.setCursor(QtCore.Qt.PointingHandCursor)
        self.text = text
        self.enter = False
    def paintEvent(self, a0: QtGui.QPaintEvent):
        p = QPainter(self)
        font = QFont()
        metric = QFontMetrics(font)
        if self.enter:
            font.setUnderline(True)
        p.setPen(QtCore.Qt.red)
        p.setFont(font)
        p.drawText((self.width() - metric.width(self.text))/2,self.height()/2,self.text)

    def enterEvent(self, a0: QtCore.QEvent):
        self.enter = True
        self.update()

    def leaveEvent(self, a0: QtCore.QEvent):
        self.enter = False
        self.update()

class CheckButton(QPushButton):
    def __init__(self,pixcheckurl,pixuncheckurl,parent=None):
        super(CheckButton, self).__init__(parent)
        self.setCursor(QtCore.Qt.PointingHandCursor)
        self.setFixedSize(150,150)
        self.checkindex = 4
        self.uncheckindex = 0
        self.checked = True
        self.enter = False

        self.pixcheck = QPixmap(pixcheckurl)
        self.pixuncheck = QPixmap(pixuncheckurl)

        pixcheckbox = QPixmap(":/image/cat_checkbox.png")
        self.checklist = []
        for i in range(12):
            self.checklist.append(pixcheckbox.copy(i*(pixcheckbox.width()/12),0,pixcheckbox.width(),pixcheckbox.height()))
    def paintEvent(self, a0: QtGui.QPaintEvent):
        p = QPainter(self)
        p1 = QPainter(self)
        self.pixcheckhover = QPixmap(":/image/cat_hover.png")
        self.pixuncheckhover = QPixmap(":/image/cat_grayi_hover.png")


        if self.checked:
            if self.enter:
                p.drawPixmap(self.rect(),self.pixcheckhover)
                p.drawPixmap(110,110,self.checklist[self.checkindex])
            p1.drawPixmap(self.rect(),self.pixcheck)
            p1.drawPixmap(110,110,self.checklist[self.checkindex])
        else:
            if self.enter:
                p.drawPixmap(self.rect(),self.pixuncheckhover)
                p.drawPixmap(110,110,self.checklist[self.uncheckindex])
            p1.drawPixmap(self.rect(),self.pixuncheck)
            p1.drawPixmap(110,110,self.checklist[self.uncheckindex])

    def enterEvent(self, a0: QtCore.QEvent):
        self.checkindex = 5
        self.uncheckindex = 1
        self.enter = True
        self.update()

    def mousePressEvent(self, e: QtGui.QMouseEvent):
        if e.button() == QtCore.Qt.LeftButton:
            if self.checked:
                self.checked = False
            else:
                self.checked = True
            self.update()
            super(CheckButton, self).mousePressEvent(e)
    def leaveEvent(self, a0: QtCore.QEvent):
        self.enter = False
        self.checkindex = 4
        self.uncheckindex = 0
        self.update()

class MainButton4(QPushButton):
    def __init__(self,pixani,pixurl,parent=None):
        super(MainButton4, self).__init__(parent)
        anipix = QPixmap(pixani)
        norpix = QPixmap(pixurl)
        self.normallist = []
        for i in range(4):
            self.normallist.append(norpix.copy(i*(norpix.width()/4),0,norpix.width()/4,norpix.height()))
        self.animalist = []
        for i in range(8):
            self.animalist.append(anipix.copy(i*(anipix.width()/8),0,anipix.width()/8,anipix.height()))
        self.setCursor(QtCore.Qt.PointingHandCursor)
        self.animaindex = 0
        self.animation = QPropertyAnimation(self,b"")
        self.animation.setStartValue(0)
        self.animation.setEndValue(7)
        self.animation.setDuration(500)
        self.animation.valueChanged.connect(self.slot_valuechange)
        self.enter = False

    def paintEvent(self, a0: QtGui.QPaintEvent):
        p = QPainter(self)
        if self.enter:
            p.drawPixmap(self.rect(),self.animalist[self.animaindex])
        else:
            p.drawPixmap(self.rect(),self.normallist[0])

    def enterEvent(self, a0: QtCore.QEvent):
        self.enter = True
        self.animation.start()
    def leaveEvent(self, a0: QtCore.QEvent):
        self.enter = False
        self.update()
    def slot_valuechange(self,var):
        self.animaindex = var
        self.update()

class CleanMainButton(QPushButton):
    def __init__(self,normal,gray,aninormal,anigray,number,parent=None):
        super(CleanMainButton, self).__init__(parent)
        self.pixnormal = QPixmap(normal)
        self.pixgray = QPixmap(gray)

        pixani = QPixmap(aninormal)
        pixanigray = QPixmap(anigray)
        self.count = number
        self.pixaninormallist = []
        self.pixanigraylist = []
        for i in range(self.count):
            self.pixaninormallist.append(pixani.copy(i*(pixani.width()/self.count),0,pixani.width()/self.count,pixani.height()))
        for i in range(self.count):
            self.pixanigraylist.append(
                pixanigray.copy(i * (pixanigray.width() / self.count), 0, pixanigray.width() / self.count, pixanigray.height()))
        self.setCursor(QtCore.Qt.PointingHandCursor)

        self.enter = False
        self.checked = True
        self.normalindex = 0

        self.aninormal = QPropertyAnimation(self,b"")
        self.aninormal.setStartValue(0)
        self.aninormal.setEndValue(self.count - 1)
        self.aninormal.setDuration(1200)
        self.aninormal.valueChanged.connect(self.slot_normalanimchange)

    def slot_normalanimchange(self,var):
        self.normalindex = var
        self.update()

    def paintEvent(self, a0: QtGui.QPaintEvent):
        pixcheck = QPixmap(":/image/clean/group_check.png")
        pixuncheck = QPixmap(":/image/clean/group_uncheck.png")
        pixcheck_hoverborder= QPixmap(":/image/clean/select_hover.png")
        pixcheckborder = QPixmap(":/image/clean/select_normal.png")
        pixuncheck_hoverborder = QPixmap(":/image/clean/notselect_hover.png")
        pixuncheckborder = QPixmap(":/image/clean/notselect_normal.png")

        p = QPainter(self)
        p2 = QPainter(self)

        if self.enter:
            if self.checked:
                p.drawPixmap((self.width() - pixcheckborder.width()) / 2, (self.height() - pixcheckborder.height()) / 2,
                             pixcheckborder);
                p2.drawPixmap((self.width() - pixcheck_hoverborder.width()) / 2,
                              (self.height() - pixcheck_hoverborder.height()) / 2, pixcheck_hoverborder);
                p.drawPixmap((self.width() - self.pixaninormallist[self.normalindex].width()) / 2,
                             (self.height() - self.pixaninormallist[self.normalindex].height()) / 2
                             , self.pixaninormallist[self.normalindex]);
                p.drawPixmap(self.width() - pixcheck.width(), self.height() - 1.5 * (pixcheck.height()), pixcheck);
            else:
                p.drawPixmap((self.width() - pixuncheckborder.width()) / 2, (self.height() - pixuncheckborder.height()) / 2,
                             pixuncheckborder);
                p2.drawPixmap(self.rect(), pixuncheck_hoverborder);
                p.drawPixmap((self.width() - self.pixanigraylist[self.normalindex].width()) / 2,
                             (self.height() - self.pixanigraylist[self.normalindex].height()) / 2
                             , self.pixanigraylist[self.normalindex]);
                p.drawPixmap(self.width() - pixuncheck.width(), self.height() - 1.5 * (pixuncheck.height()), pixuncheck);
        else:
            if self.checked:
                p.drawPixmap((self.width() - self.pixnormal.width()) / 2, (self.height() - self.pixnormal.height()) / 2, self.pixnormal);
                p.drawPixmap(self.width() - pixcheck.width(), self.height() - 1.5 * (pixcheck.height()), pixcheck);
            else:
                p.drawPixmap((self.width() - self.pixgray.width()) / 2, (self.height() - self.pixgray.height()) / 2, self.pixgray);
                p.drawPixmap(self.width() - pixuncheck.width(), self.height() - 1.5 * (pixuncheck.height()), pixuncheck);

    def enterEvent(self, a0: QtCore.QEvent):
        self.enter = True
        self.aninormal.start()
    def mousePressEvent(self, e: QtGui.QMouseEvent):
        if e.button() == QtCore.Qt.LeftButton:
            if self.checked:
                self.checked = False
            else:
                self.checked = True
            self.aninormal.start()
            super(CleanMainButton, self).mousePressEvent(e)
    def leaveEvent(self, a0: QtCore.QEvent):
        self.enter = False
        self.aninormal.stop()
        self.normalindex = 0

class MainButton5(QPushButton):
    def __init__(self,pixnormal,text,parent=None):
        super(MainButton5, self).__init__(parent)
        self.setCursor(QtCore.Qt.PointingHandCursor)
        self.index = 0
        self.enter = False
        self.pixlist = []
        pix = QPixmap(pixnormal)
        for i in range(4):
            self.pixlist.append(pix.copy(i*(pix.width()/4),0,pix.width()/4,pix.height()))
        self.text = text
        self.setStyleSheet("main5Button{color:rgb(36,147,229)}"
                  "main5Button::hover{color:rgb(36,147,229,180)}")

    def paintEvent(self, a0: QtGui.QPaintEvent):
        painter = QPainter(self)
        painter.drawPixmap((self.width()-self.pixlist[self.index].width())/2,(self.height()-self.pixlist[self.index].height())/2,
                           self.pixlist[self.index].width(),
                           self.pixlist[self.index].height(),self.pixlist[self.index])

        font = QFont()
        metri = QFontMetrics(font)
        painter.setPen(QColor(36,147,229))

        w = metri.width(self.text)
        painter.drawText((self.width() - w)/2+5,16,self.text)
    def enterEvent(self, a0: QtCore.QEvent):
        self.index = 1
        self.update()
    def leaveEvent(self, a0: QtCore.QEvent):
        self.index = 0
        self.enter = False
        self.update()
    def mousePressEvent(self, e: QtGui.QMouseEvent):
        if e.button() == QtCore.Qt.LeftButton:
            self.index = 2
            self.update()
            super(MainButton5, self).mousePressEvent(e)
    def mouseReleaseEvent(self, e: QtGui.QMouseEvent):
        if e.button() == QtCore.Qt.LeftButton:
            self.index = 1
            self.update()
            super(MainButton5, self).mouseReleaseEvent(e)


class MainButton6(QPushButton):
    def __init__(self,pixurl,text,parent=None):
        super(MainButton6, self).__init__(parent)
        self.pix = QPixmap(pixurl)
        if self.pix == None:
            print("self.pix is null")
        else:
            print("self.pix is not null")
        self.text = text
        print("text:{}".format(self.text))
        self.enter = False
    def paintEvent(self, a0: QtGui.QPaintEvent):
        super(MainButton6, self).paintEvent(a0)
        p = QPainter(self)
        if self.enter:
            p.setBrush(QColor(242,242,242))
            p.drawRect(-1,-1,self.width()+1,self.height()+1)
        p.drawPixmap((self.width()-self.pix.width())/2,15,self.pix)
        p.setPen(QColor(88,88,88))
        font = QFont()
        metrix = QFontMetrics(font)
        w = metrix.width(self.text)
        p.drawText((self.width() - w)/2,70,self.text)

    def enterEvent(self, a0: QtCore.QEvent):
        self.enter = True
        self.update()
    def leaveEvent(self, a0: QtCore.QEvent):
        self.enter = False
        self.update()
